home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7359 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: airdmhor.gen.nz!not-for-mail
  2. From: gumboot@airdmhor.gen.nz (Simon Hosie)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to get a random strin
  5. Date: 26 Feb 1996 10:03:37 +1300
  6. Organization: Airdmhor : a couple of BBS's, a bunch of people, and a cat.
  7. Message-ID: <4gqir9$d5r@airdmhor.gen.nz>
  8. References: <4g19id$p7n@gail.ripco.com> <Dn5E0J.GKL@thinkage.on.ca>
  9. NNTP-Posting-Host: airdmhor.gen.nz
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. In article <4g19id$p7n@gail.ripco.com> mambuhl@ripco.com (Martin Ambuhl) writes:
  13. > chancl@nevada.edu (Clapton Chan) in <4fh5od$qq0@news.nevada.edu> asks:
  14. > [A poor practice follows]
  15. > if you do not #include <time.h>, you need
  16. >     srand((unsigned)time(NULL));
  17.  
  18. Alan Bowler:
  19. > Worse than poor.  time() returns a time_t which might not be an
  20. > integer type.  The above code implicitly declares "time()" as returning
  21. > int.  Adding the cast to unsigned will not change that fact that
  22. > this implicit declaration means your code could be looking in the
  23. > wrong place.  For example:  Suppose "time_t" is actually "double"
  24. > and not "int", and that the machine uses has separate floating
  25. > point and integer registers.  (x86, pdp-11, ibm/370).  Then the
  26. > above code could well result in the same vale being passed to srand()
  27. > every time the program is called, because time() set the floating
  28. > point result register and the above code is looking at an integer
  29. > result register which untouched by time().
  30.  
  31.   I don't follow that.. if time returns a float then won't the result be
  32. cast and truncated to an int?
  33.  
  34. Is the following legal, by the way?
  35.  
  36.     char Temp[sizeof(time_t) + sizeof(unsigned)];
  37.  
  38.     time((time_t *)Temp);
  39.     srand(*(unsigned *)Temp);
  40.